<template>
  <div class="app-container">
    <el-button type="primary" style="margin-bottom: 20px;" @click="openDialog(null)"><i class="el-icon-edit" />新建后台用户</el-button>
    <el-table
      :data="tableData"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column
        prop="id"
        label="id"
      />
      <el-table-column
        prop="username"
        label="昵称"
      />
      <el-table-column
        prop="role_name"
        label="角色名称"
      />
      <el-table-column
        prop="is_admin"
        label="是否是管理员"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.is_admin ? 'success' : 'primary'"
            disable-transitions
          >
            {{ scope.row.is_admin ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="openDialog(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="remove(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right;margin-top: 30px;">
      <el-pagination
        :page-sizes="[10, 20, 50, 100]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :current-page="currentPage"
        :hide-on-single-page="!total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog title="管理员编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="名称" label-width="120px">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item label="角色" label-width="120px">
          <el-select v-model="form.role_id">
            <el-option
              v-for="item in roleLists"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否是管理员" label-width="120px">
          <el-switch
            v-model="form.is_admin"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
import { list, addOrEdit, del } from '@/api/adminUser'
import { selectList } from '@/api/roles'

export default {
  inject: ['reload'],
  data() {
    return {
      tableData: [],
      form: {
        id: '',
        username: '',
        password: '',
        role_id: 0,
        is_admin: false
      },
      roleLists: [],
      dialogFormVisible: false,
      currentPage: 1,
      total: 0,
      size: 10
    }
  },
  created() {
    this.getList()
    this.rolesList()
  },

  methods: {
    getList: function() {
      list(this.currentPage, this.size).then(res => {
        const { data } = res
        this.tableData = data.list
        this.total = data.total
      })
    },

    rolesList: function() {
      selectList().then(res => {
        const { data } = res
        this.roleLists = data
      })
    },

    openDialog(data) {
      if (data === null) {
        this.form = {
          id: '',
          username: '',
          password: '',
          role_id: '',
          is_admin: false
        }
      } else {
        this.form = JSON.parse(JSON.stringify(data))
        this.form.password = ''
        this.form.is_admin = !!this.form.is_admin
      }
      this.dialogFormVisible = true
    },

    handleSizeChange(val) {
      this.size = val
      this.getList()
    },

    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    edit() {
      this.dialogFormVisible = false
      addOrEdit(this.form).then(res => {
        this.$message({
          message: res.message,
          type: 'success',
          duration: 5 * 1000
        })
        this.getList()
      })
    },

    remove(data) {
      del(data.id).then(res => {
        this.$message({
          message: res.message,
          type: 'success',
          duration: 5 * 1000
        })
        this.getList()
      })
    }
  }
}
</script>
